package xyz.bali16.module.link.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.bali16.application.core.utils.TimeUtil;
import xyz.bali16.module.link.entity.Link;
import xyz.bali16.module.link.mapper.LinkMapper;
import xyz.bali16.module.link.model.LinkAddParam;
import xyz.bali16.module.link.model.LinkPageParam;
import xyz.bali16.module.link.model.LinkUpdateParam;


/**
 * @author bali2 on 9/3/2023.
 * @version 1.0
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {
    private final LinkMapper linkMapper;

    public LinkServiceImpl(LinkMapper linkMapper) {
        this.linkMapper = linkMapper;
    }

    @Override
    public IPage<Link> getPage(LinkPageParam param) {
        IPage<?> page = new Page<>(param.getCurrent(), param.getSize());
        return this.baseMapper.getPage(page, param.getName(),param.getUrl());
    }

    @Override
    public void addLink(LinkAddParam param) throws Exception {
        Preconditions.checkNotNull(param, "LinkAddParam 参数不能为为空");
        Link link = new Link();
        link.setGmtCreated(TimeUtil.getNowTimeToSeconds());
        BeanUtils.copyProperties(param, link);
        System.out.println(link);
        save(link);
    }

    @Override
    public void updateLink(LinkUpdateParam param) throws Exception {
        Preconditions.checkNotNull(param, "LinkUpdateParam 参数不能为为空");
        Link link = new Link();
        BeanUtils.copyProperties(param, link);
        updateById(link);
    }

    @Override
    public void updateStatus(String linkId, Integer status) throws Exception {
        Preconditions.checkNotNull(linkId, "友情链接id不能为空");
        if (status == null) {
            return;
        }
        Link link = new Link();

        updateById(link);
    }

    @Override
    public void delete(String linkId) throws Exception {
        if (linkId == null) {
            return;
        }
        removeById(linkId);

    }
}
